from socket import *
import paramiko 
import os
from rknnnet import *
import time
from aip import AipSpeech
from voice_wakeup import *
from aip import AipBodyAnalysis

def get_file_content(filePath):
        with open(filePath, 'rb') as fp:
            return fp.read()

def gesdetect(path):
    APP_ID = '24364647'
    API_KEY = 'Neb1LcCbvIc3ivw8o0l4xez0'
    SECRET_KEY = 'u02CFTSiTzI3ifVUCRxgWuPqkgWDx543'
    client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
    Class = {'Face':0,'One':1,'Five':2,'Fist':3,'OK':4,'Prayer':5,'Congratulation':6,\
    'Honour':7,'Heart_single':8,'Thumb_up':9,'Thumb_down':10,'ILY':11,'Palm_up':12,\
    'Heart_1':13,'Heart_2':14,'Heart_3':15,'two':16,'three':17,'four':18,'six':19,\
    'seven':20,'eight':21,'nine':22,'Rock':23,'Insult':24}
    image = get_file_content(path)
    res1 = client.gesture(image)
    r = [0]
    s = [0,0,0,0]
    shape = [640,480]
    for i in range(res1['result_num']):
        s[0] = res1['result'][i]['left']/shape[0]
        s[1] = res1['result'][i]['top']/shape[1]
        s[2] = res1['result'][i]['width']/shape[0]+s[0]
        s[3] = res1['result'][i]['height']/shape[1]+s[1]
        if res1['result'][i]['classname'] == 'Face':
            continue
        elif res1['result'][i]['classname'] == 'Fist':
            r = [-1]
            r.extend(s)
            return r
        else:
            r[0] = r[0] + 1
            r.append(Class[res1['result'][i]['classname']])
            r.extend(s)
    return r

def getpicture(host):

    port=22                                             #sftp port
    username="pi"
    password="raspberry"
    local='/home/toybrick/Desktop/ras/vedio.jpg'       #loacl path
    remote='/home/pi/Desktop/rk3399/new.jpg'                  #target path
    sf = paramiko.Transport((host,port))
    sf.connect(username = username,password = password)
    sftp = paramiko.SFTPClient.from_transport(sf)
    if os.path.isdir(local):                                        #folder or file
        for f in sftp.listdir(remote):                             
            sftp.get(os.path.join(remote+f),os.path.join(local+f))  #download all from the folder
            sf.close()
    else:
        sftp.get(remote,local)                                     #download the file
        sf.close()
    return local




if __name__ == '__main__':
    
    HOST = "10.181.231.234"      #host of rk3399
    PORT = 3399
    BUFSIZ =1024
    ADDR = (HOST,PORT)
    #Detector = yolo()
    Detector = rockxdet()
    #Sortgar = garsorting()
    linpi = socket(AF_INET,SOCK_STREAM)
    linpi.bind(ADDR)
    linpi.listen(10)
    print ('rkpro is listening on',PORT)
    mode = 'sleepmode'
    path = './'

    while True:
        try:
            print("new connect")
            torpi, rpiaddr = linpi.accept()
            addr = rpiaddr[0]
            rpdata = torpi.recv(BUFSIZ).decode('utf-8')
            err = 0
            if not rpdata:
                continue
            
            if rpdata == 'audio':
                ret = combine('lajitong')
                if ret == 1:
                    mode = 'wake'
                elif ret == 0:
                    print('Error in api')
                    continue
                else:
                    continue
            else:
                mode = 'wake'

            if mode == 'sleepmode':
                continue
            elif mode == 'wake':
                data = 'wake'
                torpi.send(data.encode())
                rpdata = torpi.recv(BUFSIZ).decode('utf-8')
                if not rpdata:
                    continue
                mode = rpdata
            else:
                print(mode+' Error')
                continue

            while mode == 'detectionmode':
                print("waiting for img")
                rpdata = torpi.recv(BUFSIZ).decode('utf-8')
                if not rpdata:
                    err = 1
                    break
                
                if rpdata == "detect":
                    print("getting img")
                    path = getpicture(addr)
                    print("dectecting by yolov3")
                    Detector.detect(path)
                    #data = Detector.result
                    data = Detector.rockxres
                    print(data)
                    data = str(data)
                    data = data.encode()
                    torpi.send(data)
                
                elif rpdata == "peoplefind":
                    print("dectecting by api")
                    gesrt = gesdetect(path)
                    data = str(gesrt)
                    data = data.encode()
                    torpi.send(data)
                                
                elif rpdata == "garbagemode":
                    mode = 'garbagemode'

                else:
                    print("rpdata error")
                    err = 1
                    break
            
            if err == 1:
                continue

            if mode == 'garbagemode':
                
                print("waiting for instruction")           
                ret = combine('fenlei')
                if ret == 1:
                    data = 'ready'.encode()
                    torpi.send(data)
                elif ret == 0:
                    print('Error in api')
                    continue
                else:
                    continue
                
            if err == 1:
                continue

            print(mode)
            mode = 'sleepmode'
            torpi.close()

            
        except:
           continue

    